<?php

namespace App\Modules\MemberCard\Controllers;

use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\MemberCard\Services\CardTypeService;
use App\Modules\MemberCard\Services\MemberCardService;
use App\Modules\MemberCard\Validators\MemberCardControllerValidator;
use App\Modules\Pay\Services\PayService;
use App\Modules\System\Services\CommonService;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Validation\ValidationException;

class MemberCardController extends BaseController
{
    private $cardTypeService;
    private $memberCardService;
    private $commentService;
    private $payService;

    public function __construct(CardTypeService $cardTypeService, MemberCardService $memberCardService,CommonService $commonService,PayService $payService)
    {
        $this->cardTypeService = $cardTypeService;
        $this->memberCardService = $memberCardService;
        $this->commentService = $commonService;
        $this->payService = $payService;
    }

    public function getRules()
    {
        return MemberCardControllerValidator::validator();
    }

    /**
     * 用户申请新会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function applyMemberCard(Request $request)
    {
        try {
            $member_id = FLAuth::id();
            $type_id = $request->input('type_id');
            $code = $request->input('code');
            $name = $request->input('name');
            $mobile = $request->input('mobile');
            $sex = $request->input('sex');
            $user_id = $request->input('user_id');

            $this->memberCardService->applyMemberCard($member_id, $type_id, $code, $name, $mobile, $sex,$user_id);

            return $this->success('申请成功');
        } catch (ValidatorException $ex) {
            $msg = array_values($ex->getMessageBag()->getMessages())[0][0];
            return $this->error($msg);
        }
    }

    /**
     * 获取本店所有会员卡 分页
     * @param Request $request
     * @return mixed
     */
    public function getMemberCardListPage(Request $request)
    {
        $store_id = $request->input('store_id');
        $status = $request->input('status');
        $keywords = $request->input('keywords');
        $pageSize = $request->input('pageSize', 10);
        $member_card_list = $this->memberCardService->getMemberCardListPage($store_id, $status, $keywords, $pageSize);


        return $this->display($member_card_list, '返回成功');

    }
    /**
     * 获取本店所有会员卡及适用服务 分页
     * @param Request $request
     * @return mixed
     */
    public function getMemberCardListPageService(Request $request)
    {
        $store_id = $request->input('store_id');
        $status = $request->input('status');
        $keywords = $request->input('keywords');
        $pageSize = $request->input('pageSize', 10);
        $service_id = $request->input('service_id');
        $member_card_list = $this->memberCardService->getMemberCardListPageService($store_id, $status, $keywords, $pageSize,$service_id);


        return $this->display($member_card_list, '返回成功');

    }


	/**
	 * 审核会员卡
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function examineMemberCard(Request $request)
    {
        $operation = $request->input('operation');
        $real_amount = $request->input('real_amount');
        $present = $request->input('present');
        $payment = $request->input('payment');
        $billId = $request->input('bill_id');
        $result = $this->memberCardService->examineMemberCard($billId, $operation, $real_amount, $payment, $present);
        $memberCard = $result['member_card'];
        unset($result['member_card']);
		$this->payService->Pay($memberCard,2,$billId,$result);
        return $this->success('操作成功');

    }

	/**
	 * 充值会员卡
	 * @param Request $request
	 * @return mixed
	 * @throws ValidatorException
	 * @throws \App\Exceptions\AllValidatorException
	 * @throws \App\Exceptions\DbException
	 * @throws \App\Exceptions\NotFoundException
	 */
    public function rechargeMemberCard(Request $request)
    {
        $recharge_num = 1;
        $payment = $request->input('payment');
        $real_amount = $request->input('real_amount');
        $present = $request->input('present');
        $billId  = $request->input('bill_id');
        $result = $this->memberCardService->rechargeMemberCard($billId, $recharge_num, $payment,$real_amount,$present);
        $memberCard = $result['member_card'];
        unset($result['member_card']);
        $this->payService->Pay($memberCard,4,$billId,$result);
        return $this->success('操作成功');

    }

    /**
     * 小程序端获取个人会员卡列表
     * @param Request $request
     * @return mixed
     */
    public function getMyMemberCardList(Request $request)
    {
        $pageSize = $request->input('pageSize',10);
        $list = $this->memberCardService->getMyMemberCardList($pageSize);


        return $this->display($list, '操作成功');

    }

    /**
     * 理发师获取本人办理的会员卡
     * @param Request $request
     * @return mixed
     * @throws \Flower\Core\Repository\Exceptions\RepositoryException
     */
    public function getNotAuditedMemberCard(Request $request)
    {
        $user_id = FLAuth::id();
        $store_id = $request->input('store_id');
        $status = $request->input('status');
        $pageSize = $request->input('pageSize',10);
        $keywords = $request->input('keywords');

        $list = $this->memberCardService->getNotAuditedMemberCard($user_id,$store_id,$status,$pageSize,$keywords);
        return $this->display($list, '操作成功');

    }


    /**
     * 理发师获取本人未办理会员卡数
     * @param Request $request
     * @return mixed
     */
    public function getNotAuditedMemberCardNum(Request $request)
    {
        $user_id = FLAuth::id();
        $store_id = $request->input('store_id');

        $list = $this->memberCardService->getStoreMemberCradNum($store_id,0,$user_id);
        return $this->display($list, '操作成功');
    }

    /**
     * 小程序获取会员卡app
     * @param Request $request
     * @return mixed
     */
    public function getMemberCard(Request $request)
    {
        $member_card_id = $request->input('member_card_id');
        $info = $this->memberCardService->getMemberCard($member_card_id);

        return $this->display($info, '操作成功');
    }

    /**
     * 小程序获取会员卡详细信息
     * @param Request $request
     * @return mixed
     */
    public function getMemberCardInfo(Request $request)
    {
        $member_card_id = $request->input('member_card_id');
        $info = $this->memberCardService->getMemberCardInfo($member_card_id);

        return $this->display($info, '操作成功');
    }

    /**
     * 小程序获取会员卡关联服务
     * @param Request $request
     * @return mixed
     */
    public function getMemberCardServiceList(Request $request)
    {
        $card_type_id = $request->input('card_type_id');
        $list = $this->memberCardService->getMemberCardServiceList($card_type_id);

        return $this->display($list, '查询成功');
    }

    /**
     * 小程序获取本人领取会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getMyReceiveMemberCardList(Request $request)
    {
        $phone = $request->input('phone');
        $code = $request->input('code');
        $pageSize = $request->input('pageSize',10);
        $list = $this->memberCardService->getMyReceiveMemberCardList($phone,$code,$pageSize);

        return $this->display($list, '查询成功');
    }

    /**
     * 领取会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function receiveMemberCard(Request $request)
    {
        $member_card_id = $request->input('member_card_id');
        $list = $this->memberCardService->receiveMemberCard($member_card_id);

        return $this->display($list, '领取成功');
    }

    /**
     * 新增会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function createMemberCard(Request $request)
    {
        $store_id = $request->input('store_id');
        $type_id = $request->input('type_id');
        $name = $request->input('name');
        $sex = $request->input('sex');
        $mobile = $request->input('mobile');
        $code= $request->input('code');


//        $check = $this->commentService->checkSmsCode($mobile, 5, $code);
//	    if (!$check) {
//		    return $this->error("手机验证码错误！");
//	    }
        $data = $this->memberCardService->createMemberCard($store_id, $type_id, $name, $mobile, $sex,$code);

        return $this->display($data,'添加成功');
    }


    /**
     * 获取本店会员卡数量
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getStoreMemberCradNum(Request $request)
    {
        $store_id = $request->input('store_id');
        $status = $request->input('status');
        $user_id = $request->input('user_id');
        $data = $this->memberCardService->getStoreMemberCradNum($store_id, $status, $user_id);

        return $this->display($data, '查询成功');
    }


    /**
     * 转移会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function transferMemberCard(Request $request)
    {
        $store_id = $request->input('store_id');
        $card_type_id = $request->input('card_type_id');
        $name = $request->input('name');
        $mobile = $request->input('mobile');
        $sex = $request->input('sex');
        $amount = $request->input('amount');

        $this->memberCardService->transferMemberCard($store_id, $card_type_id, $name, $mobile, $sex, $amount);

        return $this->success('操作成功');
    }

	/**
	 * 获取会员转移列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
    public function getTransferMemberCardList(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);
        $keywords = $request->input('keywords', null);

        $data = $this->memberCardService->getTransferMemberCardList($store_id, $pageSize, $keywords);

        return $this->display($data, '查询成功');
    }

    /**
     * 删除转移会员卡
     * @param Request $request
     */
    public function delTransferMemberCard(Request $request)
    {
        $member_card_id = $request->input('member_card_id');

        $this->memberCardService->delTransferMemberCard($member_card_id);

        return $this->success('操作成功');
    }

	/**
	 * 编辑转移会员卡
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\AllValidatorException
	 */
    public function editTransferMemberCard(Request $request)
    {
        $member_card_id = $request->input('member_card_id');
        $card_type_id = $request->input('card_type_id');
        $name = $request->input('name');
        $mobile = $request->input('mobile');
        $sex = $request->input('sex');
        $amount = $request->input('amount');
        $this->memberCardService->editTransferMemberCard($member_card_id, $card_type_id, $name, $mobile, $sex, $amount);

        return $this->success('操作成功');
    }

	/**
	 * * 获取会员卡消费记录
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author: ranghu
	 */
    public function getMemberCardFlow(Request $request)
    {
        $member_card_id = $request->input('member_card_id');
        $pageSize = $request->input('pageSize', 10);

        $data = $this->memberCardService->getMemberCardFlow($member_card_id, $pageSize);

        return $this->display($data, '查询成功');
    }


	/**
	 * 会员卡支付流水
	 * @param Request $request
	 * @return mixed
	 * @author happydong
	 */
    public function getMemberCardFlowNew(Request $request){
	    $member_card_id = $request->input('member_card_id');
	    $pageSize = $request->input('pageSize', 10);
	    $data = $this->memberCardService->getMemberCardFlowNew($member_card_id, $pageSize);
	    return $this->display($data, '查询成功');
    }





    /**
     * 备份会员卡
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function backupsMemberCard(Request $request)
    {
        $user_id = FLAuth::id();
        $store_id = $request->input('store_id');
        $path = $this->memberCardService->backupsMemberCard($user_id, $store_id);
        return $this->display($path);

    }

    /**
     * 获取备份记录
     * @param Request $request
     * @return mixed
     */
    public function getBackupsMemberCard(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);
        $path = $this->memberCardService->getBackupsMemberCard($store_id, $pageSize);
        return $this->display($path);

    }

    /**
     * 删除会员卡
     * @param Request $request
     * @return mixed
     */
    public function deleteMemberCard(Request $request)
    {
        $store_id  = $request->input('store_id');
        $member_card_id  = $request->input('member_card_id');
        $this->memberCardService->deleteMemberCard($store_id, $member_card_id);
        return $this->success('删除成功');

    }



}
